home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 January - Disc 2 / Macworld (1999-01) (Disk 2).dmg / Serious Demos / Symbolic Composer 4.2 / Environment / Projects / Questions & Answers / Q&A Programming Functions / Programming automatas < prev    next >
Encoding:
Text File  |  1998-10-26  |  1.9 KB  |  74 lines  |  [TEXT/ScoM]

  1. PROGRAMMING AUTOMATAS
  2.  
  3. This makes interesting cellular automata sequences. This is the original 
  4. from Common Music 
  5.  
  6. (defun automaton (p1 p2 p3)
  7.   (let* ((w (- (- p3 p2))))
  8.     (incf w (if (>= w 0) -12 12))
  9.     (incf w p1)
  10.     (cond ((< w 36)
  11.            (+ w (* 12 (random 4)) 2))
  12.           ((> w 98)
  13.            (- w (* 12 (random 4)) -2))
  14.           (t (1+ w)))))
  15.  
  16. ; version modified by Paul Berg for a smaller range
  17. ; adapted to SCOM by Peter Stone
  18.  
  19. (defun automaton (p1 p2 p3)
  20.   (let (next)
  21.     (setf next
  22.           (let ((w (- (- p3 p2))))
  23.             (incf w (if (>= w 0) -1 2))
  24.             (incf w p1)
  25.             (cond ((< w 36)
  26.                    (+ w (* 2 (get-random 0 4)) 2))
  27.                   ((> w 98)
  28.                    (- w (* 2 (get-random 0 4)) -2))
  29.                   (t (+ 1 w)))))
  30.     (setf p3 p2)
  31.     (setf p2 p1 )
  32.     (setf p1 next)))
  33.  
  34. ; run automaton n times with intial parameters p1 p2 p3
  35.  
  36. (defun gen-automaton (seed n p1 p2 p3)
  37.   (let ((result nil))
  38.     (init-rnd seed)
  39.     (dotimes (i n)
  40.       (push (automaton p1 p2 p3) result))
  41.     (nreverse result)))
  42.  
  43. ; example
  44. ; (gen-automaton 0.2123 100 10 20 30)
  45.  
  46. The one modified by Paul Berg died in 100 iterations. If you view this 
  47. in visualizer you'll see it makes a bit high values, but you can  
  48. rescale them with vector-scale, vector-round or vector-offset. 
  49.  
  50. ; run automaton n times with intial parameters p1 p2 p3
  51.  
  52. (defun gen-automaton (seed n p1 p2 p3)
  53.   (let (result next)
  54.     (init-rnd seed)
  55.     (dotimes (i n)
  56.       (setq next
  57.             (let ((w (- (- p3 p2))))
  58.               (incf w (if (>= w 0) -12 12))
  59.               (incf w p1)
  60.               (cond ((< w 36)
  61.                      (+ w (* 12 (get-random 0 4)) 2))
  62.                     ((> w 98)
  63.                      (- w (* 12 (get-random 0 4)) -2))
  64.                     (t (1+ w)))))
  65.       (setq p3 p2)
  66.       (setq p2 p1)
  67.       (setq p1 next)
  68.       (push next result))
  69.     (nreverse result)))
  70.  
  71. ; example
  72. ; (gen-automaton 0.2123 100 10 20 30)
  73.  
  74.